package com.jwgf.smarowindingmachine_unity.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.jwgf.group.Group;
import com.jwgf.group.GroupMap;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.objectbox.SpindleParameter_;

import java.util.List;

public class CraftCreateDialog extends DialogFragment {
    public static String KEY = "parameter_id";
    private Runnable callback;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final long id = getArguments().getLong(KEY);
        final SpindleParameter parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        final EditText value = new EditText(getContext());

        value.setText(parameter.craftName);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(value);
        builder.setPositiveButton("改名", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = value.getText().toString();
                if (!TextUtils.isEmpty(name)) {
                    parameter.craftName = name;
                    ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    if (callback != null) {
                        callback.run();
                    }
                } else {
                    Toast.makeText(getContext(), "批次名不能为空！", Toast.LENGTH_LONG).show();
                }
            }
        });

        builder.setNeutralButton("新建", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = value.getText().toString();
                if (!TextUtils.isEmpty(name)) {
                    long count = ObjectBox.get().boxFor(SpindleParameter.class).query().equal(SpindleParameter_.craftName, name).build().count();
                    if (count > 0) {
                        Toast.makeText(getContext(), "该批次名已存在，请修改名字！", Toast.LENGTH_LONG).show();
                    } else{
                        parameter.id = 0;
                        parameter.craftName = name;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                        if (callback != null) {
                            callback.run();
                        }
                    }
                } else {
                    Toast.makeText(getContext(), "批次名不能为空！", Toast.LENGTH_LONG).show();
                }
            }
        });
        builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                List<Group> groups = GroupMap.getInstance().getGroupList();
                boolean isApply = false;
                for (Group group: groups) {
                    if(group.getId() == id) {
                        isApply = true;
                        break;
                    }
                }
                if (isApply) {
                    Toast.makeText(getContext(), "不能删除已应用的批次！", Toast.LENGTH_LONG).show();
                } else {
                    ObjectBox.get().boxFor(SpindleParameter.class).remove(id);
                    if (callback != null) {
                        callback.run();
                    }
                }
            }
        });
        builder.setTitle("请输入批次名称");

        return builder.create();
    }

    public void setCallback(Runnable callback) {
        this.callback = callback;
    }
}
